// ConfigTableUnits.cpp : implementation file
//

#include "stdafx.h"
#include "pdarx.h"
#include "ConfigTableUnits.h"
#include "..\uesoftshare\user.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// ConfigTableUnits dialog


ConfigTableUnits::ConfigTableUnits(CWnd* pParent /*=NULL*/)
	: CDialog(ConfigTableUnits::IDD, pParent)
{
	//{{AFX_DATA_INIT(ConfigTableUnits)
	m_ADis = _T("");
	m_F = _T("");
	m_Leng = _T("");
	m_LineDis = _T("");
	m_M = _T("");
	m_PipeDia = _T("");
	m_Press = _T("");
	m_Press1 = _T("");
	m_Thick = _T("");
	m_T = _T("");
	m_EModulus = _T("");
	m_pRs=NULL;
	m_pRsUnitScaleTip=NULL;
	::AfxSetResourceHandle(g_ARXInstance);
	//}}AFX_DATA_INIT
}


void ConfigTableUnits::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ConfigTableUnits)
	DDX_Control(pDX, IDC_THICK_COMBO, m_ThickCombo);
	DDX_Control(pDX, IDC_T_COMBO, m_T_Combo);
	DDX_Control(pDX, IDC_PRESS1_COMBO, m_Press1Combo);
	DDX_Control(pDX, IDC_PRESS_COMBO, m_PressCombo);
	DDX_Control(pDX, IDC_PIPEDIA_COMBO, m_PipeDiaCombo);
	DDX_Control(pDX, IDC_M_COMBO, m_M_Combo);
	DDX_Control(pDX, IDC_LINEDIS_COMBO, m_LineDisCombo);
	DDX_Control(pDX, IDC_LENG_COMBO, m_Leng_Combo);
	DDX_Control(pDX, IDC_F_COMBO, m_F_Combo);
	DDX_Control(pDX, IDC_ADIS_COMBO, m_ADis_Combo);
	DDX_Control(pDX, IDC_E_COMBO, m_EModulus_ComBo);
	
	DDX_CBString(pDX, IDC_ADIS_COMBO, m_ADis);
	DDX_CBString(pDX, IDC_F_COMBO, m_F);
	DDX_CBString(pDX, IDC_LENG_COMBO, m_Leng);
	DDX_CBString(pDX, IDC_LINEDIS_COMBO, m_LineDis);
	DDX_CBString(pDX, IDC_M_COMBO, m_M);
	DDX_CBString(pDX, IDC_PIPEDIA_COMBO, m_PipeDia);
	DDX_CBString(pDX, IDC_PRESS_COMBO, m_Press);
	DDX_CBString(pDX, IDC_PRESS1_COMBO, m_Press1);
	DDX_CBString(pDX, IDC_THICK_COMBO, m_Thick);
	DDX_CBString(pDX, IDC_T_COMBO, m_T);
	DDX_CBString(pDX,  IDC_E_COMBO,m_EModulus);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ConfigTableUnits, CDialog)
	//{{AFX_MSG_MAP(ConfigTableUnits)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ConfigTableUnits message handlers

void ConfigTableUnits::OnSave() 
{
	CString strSQL=_T("");
	try
	{
		if(m_pRs->State==adStateOpen)
		{
			m_pRs->Close();
		}
		OpenTableUnits();
		SaveUnits(m_ADis_Combo,"AngleDispUnitName");
		SaveUnits(m_F_Combo,"ForceUnitName");			
		SaveUnits(m_Leng_Combo,"LenUnitName");			
		SaveUnits(m_LineDisCombo,"LineDispUnitName");			
		SaveUnits(m_M_Combo,"OMomentUnitName");			
		SaveUnits(m_PipeDiaCombo,"DiaUnitName");			
		SaveUnits(m_PressCombo,"PreUnitName");			
		SaveUnits(m_Press1Combo,"StressUnitName");			
		SaveUnits(m_T_Combo,"ScUnitName");			
		SaveUnits(m_ThickCombo,"ThkUnitName");	
		SaveUnits(m_EModulus_ComBo,"EModulusUnitName");
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
	CDialog::OnOK();
}

void ConfigTableUnits::OnCancel() 
{
	
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}
void ConfigTableUnits::OpenUnitScaleTip()
{
	CString strSQL=_T("");
	try
	{
		strSQL=_T("select * from UnitScaleTip");
		m_pRsUnitScaleTip.CreateInstance(__uuidof(Recordset));
		m_pRsUnitScaleTip->Open(_variant_t(strSQL),(IDispatch*)g_pConShareRefInfo,adOpenStatic,adLockOptimistic,adCmdText);
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
		throw;
	}
}

void ConfigTableUnits::OpenTableUnits()
{
	CString strSQL=_T("");
	try
	{
		strSQL=_T("select * from TableUnits");
		if(m_pRs==NULL)
		{
			m_pRs.CreateInstance(__uuidof(Recordset));
		}
		m_pRs->Open(_variant_t(strSQL),(IDispatch*)g_pConSort,adOpenStatic,adLockOptimistic,adCmdText);
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
		throw;
	}
}

BOOL ConfigTableUnits::OnInitDialog() 
{
	CDialog::OnInitDialog();
	try
	{
		OpenUnitScaleTip();
		OpenTableUnits();
		GetUnitsDefault();
		AddItem(m_ADis_Combo,"AngleDispUnitName",m_ADis);
		AddItem(m_F_Combo,"ForceUnitName",m_F);			
		AddItem(m_Leng_Combo,"LenUnitName",m_Leng);			
		AddItem(m_LineDisCombo,"LineDispUnitName",m_LineDis);			
		AddItem(m_M_Combo,"OMomentUnitName",m_M);			
		AddItem(m_PipeDiaCombo,"DiaUnitName",m_PipeDia);			
		AddItem(m_PressCombo,"PreUnitName",m_Press);			
		AddItem(m_Press1Combo,"StressUnitName",m_Press1);			
		AddItem(m_T_Combo,"ScUnitName",m_T);			
		AddItem(m_ThickCombo,"ThkUnitName",m_Thick);	
		AddItem(m_EModulus_ComBo,"EModulusUnitName",m_EModulus);
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
	}
	return true;
}

void ConfigTableUnits::AddItem(CComboBox& CBox,CString Fields,CString& val)
{
	CString str=_T("");
	int nCurRow=0;
	try
	{
		if(m_pRsUnitScaleTip->GetRecordCount()>0)
		{
			m_pRsUnitScaleTip->MoveFirst();
		}
		while(!m_pRsUnitScaleTip->adoEOF&&!m_pRsUnitScaleTip->BOF)
		{
			str=vtos(m_pRsUnitScaleTip->GetCollect(_variant_t(Fields)));
			if(CBox.FindString(-1,(LPCTSTR)str)==CB_ERR)
			{
				CBox.AddString((LPCTSTR)str);
			}
			m_pRsUnitScaleTip->MoveNext();
		}
		if(CBox.FindString(-1,LPCTSTR(val))!=CB_ERR)
		{
			nCurRow= CBox.FindString(-1,val);
		}
		if(m_pRsUnitScaleTip->GetRecordCount()>0)
		{
		    CBox.SetCurSel(nCurRow);
		}
	}
	catch(_com_error e)
	{
		throw;
	}
}

void ConfigTableUnits::GetUnitsDefault()
{
	try
	{
		if(m_pRs->GetRecordCount()>0)
		{
			m_F=vtos(m_pRs->GetCollect(_T("ForceUnitName")));
			m_ADis=vtos(m_pRs->GetCollect(_T("AngleDispUnitName")));
			m_Leng=vtos(m_pRs->GetCollect(_T("LenUnitName")));
			m_LineDis=vtos(m_pRs->GetCollect(_T("LineDispUnitName")));
			m_M=vtos(m_pRs->GetCollect(_T("OMomentUnitName")));
			m_PipeDia=vtos(m_pRs->GetCollect(_T("DiaUnitName")));
			m_Press=vtos(m_pRs->GetCollect(_T("PreUnitName")));
			m_Press1=vtos(m_pRs->GetCollect(_T("StressUnitName")));
			m_T=vtos(m_pRs->GetCollect(_T("ScUnitName")));
			m_Thick=vtos(m_pRs->GetCollect(_T("ThkUnitName")));
			m_EModulus = vtos( m_pRs->GetCollect(_T("EModulusUnitName")) );
		}
	}
	catch(_com_error e)
	{
		throw;
	}
}

void ConfigTableUnits::SaveUnits(CComboBox& CBox,CString Fields)
{
	int iCur=-1;
	CString strVal=_T("");
	try
	{
		iCur=CBox.GetCurSel();
		if(iCur!=-1)
		{
			CBox.GetLBText(iCur,strVal);
			strVal.TrimLeft();
			strVal.TrimRight();
			if(strVal!="")
			{
				if(m_pRs->GetRecordCount()==0)
				{
					m_pRs->AddNew();
				}
				m_pRs->PutCollect(_variant_t(Fields),_variant_t(strVal));
				m_pRs->Update();
			}
		}
	}
	catch(_com_error e)
	{
		throw;
	}
}


int ConfigTableUnits::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	::AfxSetResourceHandle(g_CADInstance);
	
	return 0;
}
